clear all
set more off

global chemin "HERE indicated path to the main folder"
cd "$chemin\data"

use final_IHME22, clear
keep if donor == "France"
drop if recipient_name == "Unallocated/Unspecified"
* we drop 2019 and 2020 because we don't have bilateral data but just global data
drop if year >= 2019
gen MCP = 0
replace MCP = 1 if year <1993 /* actif jusqu'en 12/1992 inclus */
replace MCP = 1 if year == 1995
replace MCP = 1 if year >1999 & year <2009 /* actif en 1999/2000 et à partir du 01/2001 jusqu'en 12/2008*/
replace MCP = 1 if year >2016 /*actif 01/2017 - 12/2020*/ 


* Intsrument construction
	* Part 1 : probability of receiving FP aid when MCP is not active
	gen aid_pc = (rmh_fp_dah_20_US >0)
	bys recipient_name : egen aid = mean(aid_pc) if MCP == 0
	bys recipient_name : egen FPr = mean(aid)

* instrument 1 : Probability of receiving FP aid when MCP is not active * share channelled through NGO during OBAMA term * policy
	gen instrument1 = FPr * share_NGOUS_Obama * MCP
* instrument 2 : Probability of receiving FP aid when MCP is not active * share channelled through NGO when MCP not active * policy
	gen instrument2 = FPr * share_NGOUS_MCP * MCP

* Creation of controls
	gen lngdppc = ln(GDPpc_r)
	gen lnpopr = ln(pop_r)
	gen laidfp = ln(rmh_fp_dah_20 + ((rmh_fp_dah_20^2 +1)^0.5))
	gen laidUS = ln(rmh_fp_dah_20_US + ((rmh_fp_dah_20_US^2 +1)^0.5))

	gen laidfp2 = ln(rmh_fp_dah_20 + 1)
	gen laidUS2 = ln(rmh_fp_dah_20_US + 1)

	egen id_o = group(donor)
	egen id_dr = group(donor recipient_name)
	egen id_r = group( recipient_name)
	gen fert2 = fertility_r *fertility_r

xtset id_dr year
* Définition of different set of controls 
global controlsi  L.lngdppc* L.lnpopr* L.poverty_r*    																				
global controls1i L.lngdppc* L.lnpopr* L.poverty_r* L.life_expectencyfe_r* L.HIV1549_r* L.incidenceHIV_r* L.fert2* L.fertility* L.adofertility* L.maternalmortality_r*
global controls2i L.lngdppc* L.lnpopr* L.poverty_r* L.life_expectencyfe_r* L.HIV1549_r* L.incidenceHIV_r* L.fert2* L.fertility* L.adofertility* L.maternalmortality_r* L.polity2* 


reghdfe laidUS $controls2i MCP, absorb(id_d) cluster(recipient_name) 
gen in_sample4 =e(sample)

reghdfe laidUS MCP if in_s == 1, absorb(id_d) cluster(recipient_name) 
est store B1
estadd local DRFE "Yes": B1
estadd local DYFE "Yes": B1
foreach i of numlist 1/3 {
	estadd local control`i' "No": B1
}
foreach i of numlist 4/6 {
	estadd local control`i' "No": B1
}	
foreach i of numlist 7/10 {
	estadd local control`i' "No": B1
}
foreach i of numlist 11/12 {
	estadd local control`i' "No": B1
}
reghdfe laidUS $controlsi MCP if in_s == 1, absorb(id_d) cluster(recipient_name) 
est store B2
estadd local DRFE "Yes": B2
estadd local DYFE "Yes": B2
foreach i of numlist 1/3 {
	estadd local control`i' "Yes": B2
}
foreach i of numlist 4/6 {
	estadd local control`i' "No": B2
}	
foreach i of numlist 7/10 {
	estadd local control`i' "No": B2
}
foreach i of numlist 11/12 {
	estadd local control`i' "No": B2
}
reghdfe laidUS $controls1i MCP if in_s == 1, absorb(id_d) cluster(recipient_name) 
est store B3
estadd local DRFE "Yes": B3
estadd local DYFE "Yes": B3
foreach i of numlist 1/3 {
	estadd local control`i' "Yes": B3
}
foreach i of numlist 4/6 {
	estadd local control`i' "Yes": B3
}	
foreach i of numlist 7/10 {
	estadd local control`i' "No": B3
}
foreach i of numlist 11/12 {
	estadd local control`i' "No": B3
}
reghdfe laidUS $controls2i MCP if in_s == 1, absorb(id_d) cluster(recipient_name) 
est store B4
estadd local DRFE "Yes": B4
estadd local DYFE "Yes": B4
foreach i of numlist 1/3 {
	estadd local control`i' "Yes": B4
}
foreach i of numlist 4/6 {
	estadd local control`i' "Yes": B4
}	
foreach i of numlist 7/10 {
	estadd local control`i' "Yes": B4
}
foreach i of numlist 11/12 {
	estadd local control`i' "No": B4
}


* 2nd Stage*
esttab B1 B2 B3 B4, keep(MCP)
esttab B1 B2 B3 B4 /*2SLS*/  using "..\output\USeffect.tex" ,  replace ///
 keep(MCP L.lngdppc L.lnpopr L.poverty_r L.life_expectencyfe_r L.incidenceHIV_r L.HIV1549_r L.fertility_r L.fert2 L.adofertility_r L.maternalmortality_r L.polity2 )  ///
order(MCP L.lngdppc L.lnpopr L.poverty_r L.life_expectencyfe_r L.incidenceHIV_r L.HIV1549_r L.fertility_r L.fert2 L.adofertility_r L.maternalmortality_r L.polity2 ) ///
mgroups("Dep. Var: US Family planning aid" , pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) nomtitles ///
coeflabel(MCP "Mexico City Policy is active"  ///
L.lngdppc "ln(GDP per capita)\$_{rt-1}$"  ///
L.lnpopr "ln(Population)\$_{rt-1}$"  ///
L.polity2 "Democratic Index\$_{rt-1}$"  ///
L.life_expectencyfe_r "Female life expectancy\$_{rt-1}$"  ///
L.poverty_r "Poverty ratio\$_{rt-1}$"  ///
L.incidenceHIV_r "HIV incidence (15-49)\$_{rt-1}$"  ///
L.HIV1549_r "HIV prevalence (15-49)\$_{rt-1}$"  ///
L.fertility_r "Fertility rate\$_{rt-1}$"  ///
L.fert2 "Fertility rate\$^{2}_{rt-1}$"  ///
L.maternalmortality_r "Maternal mortality rate\$_{rt-1}$"  ///
L.adofertility_r "Adolescent fertility rate\$_{rt-1}$"  ) ///
scalars("N Obs." "N_clust1 Recipient" ) sfmt(a3) ///
b(a3) se(a3) starlevels({$^{*}$} 0.10 {$^{**}$} 0.05 {$^{***}$} 0.01) /// /* number format and stars */
  substitute($ \$) /// /* Formatting label */
nonumber compress nolines nogap title() booktabs fragment  noobs nonum nonotes collabels(none) se  /* some options on formatings */

